// __PUBLIC__/js/page/event/admin/eventlist
require(['../../common'], function() {
  require(['admin.eventlist'])
})

define('admin.eventlist', [
  'jquery',
  'page/grid',
  'css!ke/themes/default/default.css',
  'ke/kindeditor',
], function($, Grid) {
  'use strict';


  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init();

  function edit(id, act) {
    if (act == 'recommend') {
      var v = '推荐',
        v2 = '取消推荐',
        act2 = 'cancel'
    } else {
      var v = '取消推荐',
        v2 = '推荐',
        act2 = 'recommend'
    }
    if (confirm('是否' + v)) {
      $.post(APP + '?app=event&m=Admin&a=recommendEvent', {
        id: id,
        type: act
      }).done(function(text) {
        if (text === '1') {
          alert('操作成功')
          $('#button' + id).html('<a href="javascript:void(0);" data-id="' + id + '" data-act="' + act2 + '" class="A_edit">' + v2 + '</a>');
        } else {
          alert('操作失败')
        }
      })
    }
  }

  function del(selected) {
    $.post(APP + '?app=event&m=Admin&a=deleteEvent', {
      id: selected.id.join()
    }, function(text) {
      if (text === '1') {
        mygrid.del(selected.index)
      } else if (text === '2') {
        mygrid.del(selected.index)
      } else {
        alert('删除失败')
      }
    });
  }

  KindEditor('.A_shift').click(function() {
    var id = $(this).closest('tr').data('id')
    var url = APP + '?app=event&m=Admin&a=transferEventType&id=' + id

    var dialog = KindEditor.dialog({
      width: 500,
      height: 150,
      title: '活动分类转移',
      body: '<iframe id="content-frame" name="contentFrame" frameborder="0" src="' + url + '"></iframe>',
      closeBtn: {
        name: '关闭',
        click: function(e) {
          dialog.remove();
        }
      },
      yesBtn: {
        name: '确定',
        click: function(e) {
          var type = $('#content-frame').contents().find('#J_transfer').val()
          id = $('#content-frame').contents().find('#id').val()

          if (type == 0) {
            alert('请选择活动分类')
          } else if (confirm('确定转移活动？')) {
            $.post(APP + '?app=event&m=Admin&a=doTransferEventType', {
              id: id,
              type: type
            }).done(function(text) {
              if (text === '1') {
                alert('转移多个活动成功')
                dialog.remove()
                location.reload()
              } else if (text === '2') {
                alert('转移成功')
                dialog.remove()
                location.reload()
              } else {
                alert('转移失败')
              }
            })
          }
        }
      },
      noBtn: {
        name: '取消',
        click: function(e) {
          dialog.remove();
        }
      }
    });
  })

  KindEditor('#J_transfer').click(function() {

    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert('请选择一条数据')
    } else {
      var id = $(this).closest('tr').data('id')
      var url = APP + '?app=event&m=Admin&a=transferEventType&id=' + selected.id.join()

      var dialog = KindEditor.dialog({
        width: 500,
        height: 150,
        title: '活动分类转移',
        body: '<iframe id="content-frame" name="contentFrame" frameborder="0" src="' + url + '"></iframe>',
        closeBtn: {
          name: '关闭',
          click: function(e) {
            dialog.remove();
          }
        },
        yesBtn: {
          name: '确定',
          click: function(e) {
            var type = $('#content-frame').contents().find('#J_transfer').val()
            id = $('#content-frame').contents().find('#id').val()

            if (type == 0) {
              alert('请选择活动分类')
            } else if (confirm('确定转移活动？')) {
              $.post(APP + '?app=event&m=Admin&a=doTransferEventType', {
                id: id,
                type: type
              }).done(function(text) {
                if (text === '1') {
                  alert('转移多个活动成功')
                  dialog.remove()
                  location.reload()
                } else if (text === '2') {
                  alert('转移成功')
                  dialog.remove()
                  location.reload()
                } else {
                  alert('转移失败')
                }
              })
            }
          }
        },
        noBtn: {
          name: '取消',
          click: function(e) {
            dialog.remove();
          }
        }
      });
    }
  })

  $(document)
    .on('click', '.A_edit', function(e) {
      e.preventDefault()
      var id = $(this).closest('tr').data('id'),
        act = $(this).data('act')

        edit(id, act)
    })
    .on('click', '.A_del', function(e) {
      e.preventDefault()

      if (confirm('确定删除已选择的内容？')) {
        var current = mygrid.getCurrent(this)
        var selected = {
          id: [current.id],
          index: [current.index]
        }
        del(selected)
      }
    })
    .on('click', '#J_del', function(e) {
      e.preventDefault()

      var selected = mygrid.getSelected()
      if (!selected.count) {
        alert('请选择一条数据')
      } else {
        if (confirm('确定删除已选择的内容？')) {
          del(selected)
        }
      }
    })

})